from google.appengine.ext.webapp import template
import os
from django.core.paginator import Paginator


def render_paginator(query, current_page = 0, url = "", on_page = 10, step = 2, template_name = None):
	template.register_template_library('models.template_filters')
	if template_name == None:
		template_name = os.path.join(os.path.dirname(__file__), 'paginator.html')
	paginator = Paginator(query, on_page)
	if current_page == "":
		current_page = 0
	current_page = int(current_page) - 1
	paginator = ObjectPaginator(query, on_page)
	if current_page >= paginator.pages:
		current_page = paginator.pages - 1
	if current_page < 0:
		current_page = 0
	(current_page < paginator.pages - 1)
	page_from = current_page - step
	if page_from < 0:
		page_from = 0
	page_to = current_page + step + 1
	if page_to > paginator.pages:
		page_to = paginator.pages
	values = {
		'paginator_url': url,
		'current_page': current_page + 1,
		'count_pages': paginator.pages,
		'pages': range(page_from + 1, page_to + 1),
		'show_next': (current_page < paginator.pages - 1),
		'show_previous': current_page != 0, 
	}
	return template.render(template_name, values)
